//
//  MTool.m
//  SSC
//
//  Created by 红沙尘 on 17/6/9.
//  Copyright © 2017年 红沙尘. All rights reserved.
//

#import "MTool.h"
#import "AppDelegate.h"
#import "MAlertView.h"
#import <sys/sysctl.h>
#import <mach/mach.h>
#import <QuartzCore/QuartzCore.h>
#import <CoreLocation/CoreLocation.h>
#import <CommonCrypto/CommonDigest.h>
#import <SystemConfiguration/CaptiveNetwork.h>

@implementation MTool

+ (NSString *)caizhongWithType:(CaipiaoType)otype {
    
    switch (otype) {
        case CaipiaoTypeFenfencai: return @"两分彩";
        case CaipiaoTypeShishicai: return @"重庆时时彩";
        case CaipiaoTypeUnknow: return @"";
    }
}

+ (NSString *)longhuheCode:(NSString *)code {
    code = [code stringByReplacingOccurrencesOfString:@"0" withString:@"龙"];
    code = [code stringByReplacingOccurrencesOfString:@"1" withString:@"虎"];
    code = [code stringByReplacingOccurrencesOfString:@"2" withString:@"和"];
    
    return code;
}

+ (NSString *)dingweidanCode:(NSString *)code place:(NSString *)place {
    
    
    NSDictionary *tmpDict = @{@"5":@"# | - | - | - | -",
                              @"4":@"- | # | - | - | -",
                              @"3":@"- | - | # | - | -",
                              @"2":@"- | - | - | # | -",
                              @"1":@"- | - | - | - | #",};
    
    NSString *wei = tmpDict[place];
    
    return [wei stringByReplacingOccurrencesOfString:@"#" withString:code];
}

+ (NSString *)MD5:(NSString *)str {
    
    const char *cStr = [str UTF8String];
    unsigned char result[16];
    CC_MD5( cStr, (int)strlen(cStr), result );
    return [NSString stringWithFormat:
            @"%02X%02X%02X%02X%02X%02X%02X%02X%02X%02X%02X%02X%02X%02X%02X%02X",
            result[0], result[1], result[2], result[3],
            result[4], result[5], result[6], result[7],
            result[8], result[9], result[10], result[11],
            result[12], result[13], result[14], result[15]
            ];
}

+ (NSString *)formartDate:(NSString *)dateStr {
    
    dateStr = [dateStr stringByReplacingOccurrencesOfString:@"年" withString:@"-"];
    dateStr = [dateStr stringByReplacingOccurrencesOfString:@"月" withString:@"-"];
    dateStr = [dateStr stringByReplacingOccurrencesOfString:@"日" withString:@""];
    
    return dateStr;
//    NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
//    dateFormatter.dateFormat = @"yyyy年MM月dd日";
//    return [dateFormatter stringFromDate:aDate];
}

+ (int)zuheFromM:(int)m n:(int)n {
    
    if (m < n) return 0;
    
    int a = 1;
    int b = 1;
    int c = 1;
    
    for (int i = m; i > 0; i --) {
        a *= i;
    }
    
    for (int i = n; i > 0; i --) {
        b *= i;
    }
    
    for (int i = m - n; i > 0; i --) {
        c *= i;
    }
    
    return a/(b*c);
}

+ (int)pailieFromM:(int)m n:(int)n {
    
    if (m < n) return 0;
    
    int a = 1;
    int b = 1;
    int c = 1;
    
    for (int i = m; i > 0; i --) {
        a *= i;
    }
    
    for (int i = m - n; i > 0; i --) {
        c *= i;
    }
    
    return a/(b*c);
}

+ (NSString *)dateToString:(NSDate *)aDate {
    NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
    dateFormatter.dateFormat = @"yyyy年MM月dd日";
    return [dateFormatter stringFromDate:aDate];
}

+ (BOOL)judgePassWordLegal:(NSString *)pass {
    
    BOOL result = false;
    if ([pass length] >= 6){
        // 判断长度大于8位后再接着判断是否同时包含数字和字符
        NSString * regex = @"^(?![0-9]+$)(?![a-zA-Z]+$)[0-9A-Za-z]{6,16}$";
        NSPredicate *pred = [NSPredicate predicateWithFormat:@"SELF MATCHES %@", regex];
        result = [pred evaluateWithObject:pass];
    }
    return result;
}

+ (CGFloat)adapterHeight:(CGFloat)height
{
    return (height/375.0f)*ScreenWidth;
}

+ (void)showMessage:(NSString *)message inView:(UIView *)view
{
    if (!view)
    {
        AppDelegate *app =  (AppDelegate *)[UIApplication sharedApplication].delegate;
        view = app.window;
    }
    
    MAlertView *alertView = [[[NSBundle mainBundle] loadNibNamed:@"MAlertView" owner:nil options:nil] objectAtIndex:0];
    alertView.userInteractionEnabled = NO;
    [alertView showWithTitle:message inView:view];
}

+ (void)showMessage:(NSString *)message inView:(UIView *)view complete:(void(^)(void))complete {
    
    if (!view)
    {
        AppDelegate *app =  (AppDelegate *)[UIApplication sharedApplication].delegate;
        view = app.window;
    }
    
    MAlertView *alertView = [[[NSBundle mainBundle] loadNibNamed:@"MAlertView" owner:nil options:nil] objectAtIndex:0];
    alertView.userInteractionEnabled = NO;
    [alertView showWithTitle:message inView:view complete:complete];
}

+ (void)showAlertController:(UIViewController *)controller
                    message:(NSString *)message {
    
    UIAlertController *alertController = [UIAlertController alertControllerWithTitle:@"" message:message preferredStyle:UIAlertControllerStyleAlert];
    
    [alertController addAction:[UIAlertAction actionWithTitle:@"OK" style:UIAlertActionStyleDestructive handler:^(UIAlertAction * _Nonnull action) {
        
    }]];
    [controller presentViewController:alertController animated:YES completion:nil];
}

+ (UIFont *)fontOfSize:(CGFloat)size {
    
    return [UIFont systemFontOfSize:size];
    //思源黑体简体
    //    return [UIFont fontWithName:@"SourceHanSansCN-Light" size:size];
}

+ (CGSize)sizeWithText:(NSString *)text font:(UIFont *)font constrainedToSize:(CGSize)size
{
    CGSize textSize = CGSizeZero;
    
    // 7.0 系统的适配处理。
    NSDictionary * tdic = [NSDictionary dictionaryWithObjectsAndKeys:font,NSFontAttributeName,nil];
    textSize = [text boundingRectWithSize:size
                                  options:NSStringDrawingUsesLineFragmentOrigin
                               attributes:tdic
                                  context:nil].size;
    
    return textSize;
}


@end
